﻿using System;
using System.Data.Linq;
using System.Linq;
using WebUI.DAL;
using System.Collections.Generic;

namespace WebUI.BL.Coupon
{
    public class CouponDetailsBL
    {
        protected void clsCouponDetailsInsert(tbl_CouponDetail coupondetailss)
        {
            using (AdlerContextDataContext adlerContextDb = new AdlerContextDataContext())
            {
                try
                {
                    tbl_CouponDetail coupondetails = new tbl_CouponDetail();
                    coupondetails.LoginId = coupondetailss.LoginId;
                    coupondetails.FromDate = coupondetailss.FromDate;
                    coupondetails.ToDate = coupondetailss.ToDate;
                    coupondetails.Reason = coupondetailss.Reason;
                    coupondetails.CreatedUser = coupondetailss.CreatedUser;
                    coupondetails.CreatedDate = DateTime.Now;
                    coupondetails.Status = coupondetailss.Status;

                    adlerContextDb.tbl_CouponDetails.InsertOnSubmit(coupondetails);
                    adlerContextDb.SubmitChanges();
                }
                catch (ChangeConflictException)
                {
                    //neu xay ra loi rollback lai
                    adlerContextDb.ChangeConflicts.ResolveAll(RefreshMode.KeepCurrentValues);
                    adlerContextDb.SubmitChanges();
                }
            }
        }

        protected void clsCouponDetailsModify(tbl_CouponDetail coupondetailss)
        {
            using (AdlerContextDataContext adlerContextDb = new AdlerContextDataContext())
            {
                try
                {
                    var coupondetails = adlerContextDb.tbl_CouponDetails.Single(d => d.CouponNo == coupondetailss.CouponNo);
                    coupondetails.LoginId = coupondetailss.LoginId;
                    coupondetails.FromDate = coupondetailss.FromDate;
                    coupondetails.ToDate = coupondetailss.ToDate;
                    coupondetails.Reason = coupondetailss.Reason;
                    coupondetails.Status = coupondetailss.Status;

                    adlerContextDb.SubmitChanges();
                }
                catch (ChangeConflictException)
                {
                    //neu xay ra loi rollback lai
                    adlerContextDb.ChangeConflicts.ResolveAll(RefreshMode.KeepCurrentValues);
                    adlerContextDb.SubmitChanges();
                }
            }
        }

        protected void clsCouponDetailsDelete(tbl_CouponDetail coupondetailss)
        {
            using (AdlerContextDataContext adlerContextDb = new AdlerContextDataContext())
            {
                try
                {
                    var coupondetails = adlerContextDb.tbl_CouponDetails.Single(d => d.CouponNo == coupondetailss.CouponNo);
                    coupondetails.Status = coupondetailss.Status;

                    adlerContextDb.SubmitChanges();
                }
                catch (ChangeConflictException)
                {
                    //neu xay ra loi rollback lai
                    adlerContextDb.ChangeConflicts.ResolveAll(RefreshMode.KeepCurrentValues);
                    adlerContextDb.SubmitChanges();
                }
            }
        }

        // Show Coupon Details.
        public List<MODEL_SP_ADMIN_SEARCH_COUPONDETAILS> Get_Admin_CouponDetails(string loginid, string status)
        {
            using (AdlerContextDataContext contextdb = new AdlerContextDataContext())
            {
                int index = 1;
                //var query = from n in contextdb.SP_ADMIN_SEARCH_COUPONDETAILS(loginid, status)
                //            select new MODEL_SP_ADMIN_SEARCH_COUPONDETAILS
                //            {
                //                Index = index++,
                //                CouponNo = n.CouponNo,
                //                LoginId = n.LoginId,
                //                FromDate = n.FromDate,
                //                ToDate = n.ToDate,
                //                Reason = n.Reason,
                //                CreatedUser = n.CreatedUser,
                //                CreatedDate = n.CreatedDate,
                //                Status = n.Status
                //            };
                return null;// query.ToList();
            }
        }

        public tbl_CouponDetail GetCouponDetailsByCouponno(int couponno, string loginid)
        {
            using (AdlerContextDataContext contextdb = new AdlerContextDataContext())
            {
                return contextdb.tbl_CouponDetails.Single(d => d.CouponNo == couponno && d.LoginId == loginid);
            }
        }
    }
}
